<?php
/**
 * array che contiene l'elenco dei linguaggi utilizzabili per le funzioni
 */
function getLanguages() {
	return array("pol"=>"polaris", "php"=>"php" );
}
/**
 * array che contiene l'elenco dei tipi restituiti dalle funzioni
 */
function getFuncType() {
	return array("V"=>"value", 
					"A1"=>"1 level array of values", 
					"A2"=>"2 levels array of values");
}
function getPrefVar() {		
	return array('V'=>'P_','A1'=>'X_','A2'=>'Y_');
}

function getLibHeader($lib,$bNote=false) {
	global $objSDAO;
	$sSQL = "SELECT l.* " .
		" FROM library l " .
		" WHERE l.library = '".addslashes($lib)."' ";
	$query = $objSDAO->query($sSQL);
	$row = $objSDAO->fetch();
	
	$sHtml = '';
	$sHtml .= "<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n";
	$sHtml .= ("<tr>" .
			"<td class=\"titl01\" width='15%'>Library</td>" .
			"<td class=\"text01\" width='85%' style='font-weight:bold;'>".$lib." - ".$row['description']."</td>" .
			"</tr>\n");
	if ($bNote && !empty($row['note'])) {
		$sHtml .= ("<tr>" .
			"<td >&nbsp;</td>" .
			"<td class=\"text01\">".nl2br($row['note'])."</td>" .
			"</tr>\n");
	}
	$sHtml .=  "</table>\n";
	return $sHtml;
}
/**
 * check script
 */
function checkScript(){
	global $objSDAO;
	$aPrefVar = array('A1'=>'X_','A2'=>'Y_','V'=>'W_');
	$errMsg = "";
	$script = sxRTrim("script");
	$function = sxRTrim("function");
	$library = sxRTrim("lbr");
	$result_type = sxRTrim("result_type");
	$initPL = "   \$aVar = array();\r\n";
	switch($result_type) {
		case "A1":
			$returnVar = "X_return";
			$initPHP = "   \$X_return = array();\r\n";
			$initPL .= "   \$aVar['X_return'] = array();\r\n";
			break;
		case "A2":
			$returnVar = "Y_return";
			$initPHP = "   \$Y_return = array();\r\n";
			$initPL .= "   \$aVar['Y_return'] = array();\r\n";
			break;
		default:
			$returnVar = "W_return";
			$initPHP = "   \$W_return = '';\r\n";
			$initPL .= "   \$aVar['W_return'] = '';\r\n";
			break;
	}
	$sep = '$';
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,14)!='variable_name_') continue;
		if (trim($vX)=='') continue;
		$kX = substr($nX,14);
		$var = $aPrefVar[sxREQ('variable_type_'.$kX)].sxREQ('variable_name_'.$kX);
		$aFuncVar[] = $var;
		$sFuncPar .= $sep.$var;
		$initPL .= "   \$aVar['$var']=$var;\n";
		$sep = ', $';
	}	
	
	
	if(sxREQ("language") == "pol"){
		//parsing dello script in Polaris Language
		$objParse = new polarisParser;
		if(!$objParse->parseFunc($script)){
			return $objParse->getError();
		} 
		if (!isset($objParse->aVariables[$returnVar])
		or $objParse->aVariables[$returnVar]['modified'] === false) {
			$errMsg .= "Polaris Script - $returnVar not computed<br>";
		}
		$sSQL = "SELECT function FROM function WHERE library IN ('common','".addslashes($library)."')";
		$objSDAO->query($sSQL,"upd");
		$aCallFunc = array();
		while($row=$objSDAO->fetch("upd")) {
			$aCallFunc[] = $row['function'];
		}
		foreach($objParse->aFunctions as $nomeFunc => $aCF) {
			if (!in_array($nomeFunc,$aCallFunc)) {
				$errMsg .= "Function '$nomeFunc' not available<br>";
			}
		}
		foreach($objParse->aVariables as $nomeVar => $aFV) {
			if ($nomeVar == $returnVar) { continue; }
			if (strpos("P_X_Y_",substr($nomeVar,0,2))!==false) {
				if (!in_array($nomeVar,$aFuncVar)) {
					$errMsg .= "function-vatriabler '$nomeVar' not found<br>";
				}
			} elseif (strpos("T_R_S_",substr($nomeVar,0,2))===false) {
				$errMsg .= "Variable '$nomeVar' not allowed<br>";
			}
		}
		if ($errMsg != "") { return $errMsg; }
		if (sxREQ('reformat')=='Y') {
			$script = $objParse->getFormatCode();
		}
		return '';
	} 
	// language) == "PHP"
	//parsing dello script in PHP
	$output = "";
	$result = 0;
	if (!defined('SERVER_FS_TEMP')) return "constant SERVER_FS_TEMP undefined";
	$funcFile = SERVER_FS_TEMP."/phpFunction.php";
	$fh = fopen($funcFile, "wb");
	if (!$fh) return "File '$funcFile' - write access denied";
	fwrite($fh, "<?php\nfunction sxFunc_$function($sFuncPar) {\r\n");
	fwrite($fh, $initPHP);
	fwrite($fh, "\r\n   ".str_replace("\n","\n   ",$script)."\r\n");
	fwrite($fh, "\r\n   return \$$returnVar;\r\n}\r\n?>");
	fclose($fh);
	//dbg_("script PHP \n".htmlentities(file_get_contents($funcFile)));
	if (defined('PHP_EXEC_PATH') && PHP_EXEC_PATH!='') {
		exec(PHP_EXEC_PATH." $funcFile", $output, $result);
		//dbg_($script."\n".print_r($output,1).$result);
	}
	if($result != 0){
		$errMsg = "($result) - Parse error : ".PHP_EXEC_PATH."<br>";
		foreach($output as $errToken){
			$errMsg .= str_replace($funcFile, "script", $errToken)."<br>";
		}
	}
	unlink($funcFile);
	return '';
} 
